package com.jsmtr.rms.controller.SystemLog;

import com.jsmtr.rms.entity.BaseUser;
import com.jsmtr.rms.entity.SystemLog;
import com.jsmtr.rms.service.SystemLog.ISystemLogService;
import com.jsmtr.rms.utils.ResultBean;
import com.jsmtr.rms.utils.RightInterceptor;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: cyx
 * @description:
 * @date: Created in 2019/4/15 18:22
 * @modified by:
 */
@Controller
public class SystemLogOperatorController {

    @Autowired
    private ISystemLogService systemLogService;

    public ISystemLogService getSystemLogService() {
        return this.systemLogService;
    }

    public void setSystemLogService(ISystemLogService systemLogService) {
        this.systemLogService = systemLogService;
    }

    @RequestMapping(value = "/SystemLog/Operator/index")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "/SystemLog/index";
    }

    @RequestMapping(value = "/SystemLog/Operator/opstatistics")
    public String opstatistics(HttpServletRequest request, HttpServletResponse response) {
        return "/SystemLog/opstatistics";
    }


    @ResponseBody
    @RequestMapping(value = "/SystemLog/Operator/getOperationJson")
    public ResultBean getOperationJson(HttpServletRequest request, HttpServletResponse response){
        ResultBean resultBean=new ResultBean();
        int perpage=1;
        int page=500;
        BaseUser user = (BaseUser)request.getSession().getAttribute(RightInterceptor.SEESION_MEMBER);
        String sperpage=request.getParameter("page");
        String spage=request.getParameter("limit");
        if (StringTools.trimNotEmpty(sperpage)){
            perpage=Integer.parseInt(sperpage);
        }
        if(StringTools.trimNotEmpty(spage)){
            page=Integer.parseInt(spage);
        }
        String cardno=request.getParameter("cardno");
        String proname=request.getParameter("proname");
        //String operatorcode=request.getParameter("userid");
        try {
                Map params=new HashMap();
                if (StringTools.trimNotEmpty(cardno)){
                    params.put("cardno",cardno);
                }
                if (StringTools.trimNotEmpty(user.getUcode())){
                    params.put("operatorcode",user.getUcode());
                }
                if(StringTools.trimNotEmpty(proname)){
                    params.put("proname",proname);
                }

                Map remap = systemLogService.getSystemListPage(perpage,page, params);
                List<Map> logs= (List<Map>) remap.get("list");
                List<SystemLog> logList=new ArrayList<>();
                if(logs.size()>0){
                    for (Map info : logs) {
                        Map map=new HashMap();
                        SystemLog log=new SystemLog();
                        String flowname=info.get("FNAME")==null?info.get("NICKNAME").toString():info.get("FNAME").toString();
                        log.setLogid(info.get("LOGID").toString());
                        log.setProid(info.get("PROID").toString());
                        log.setFid(info.get("FID").toString());
                        log.setRid(info.get("RID").toString());
                        log.setRequesturl(info.get("REQUESTURL").toString());
                        log.setLogtype(info.get("LOGTYPE").toString());
                        log.setLoginip(info.get("LOGINIP").toString());
                        log.setOperatorid(info.get("OPERATORID").toString());
                        log.setOperatorcode(info.get("OPERATORCODE").toString());
                        log.setOperatorname(info.get("OPERATORNAME").toString());
                        log.setOperattime(info.get("OPERATTIME").toString());
                        log.setLogcontent(info.get("LOGCONTENT").toString());
                        log.setUsercustom1(info.get("PRONAME").toString());
                        log.setUsercustom2(flowname);
                        log.setUsercustom3(info.get("CARDNO").toString()+"\t");
                        log.setUsercustom4(info.get("NAME").toString());
                        log.setUsercustom5(info.get("JNAME").toString());
                        /*SystemLogUtil.fillLogRsultAndRemark(flowname,log);*/
                        logList.add(log);
                    }
                }
                resultBean.setState(true);
                resultBean.setMessage("操作成功");
            Double pageCount = (Double) remap.get("pageCount");
            BigDecimal allcount = (BigDecimal) remap.get("count");
            resultBean.setPageNumber(String.valueOf(perpage));
            resultBean.setTotalResult(String.valueOf(allcount));
            resultBean.setPageSize(String.valueOf(page));
            resultBean.setTotalPage(String.valueOf(pageCount));
                resultBean.setDatas(logList);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return  resultBean;
    }


    @ResponseBody
    @RequestMapping(value = "/SystemLog/Operator/getStatisticsJson")
    public ResultBean getOperationStatisticsJson(HttpServletRequest request, HttpServletResponse response){
        ResultBean resultBean=new ResultBean();
        int perpage=1;
        int page=500;
        BaseUser user = (BaseUser)request.getSession().getAttribute(RightInterceptor.SEESION_MEMBER);
        String sperpage=request.getParameter("page");
        String spage=request.getParameter("limit");
        if (StringTools.trimNotEmpty(sperpage)){
            perpage=Integer.parseInt(sperpage);
        }
        if(StringTools.trimNotEmpty(spage)){
            page=Integer.parseInt(spage);
        }
        String cardno=request.getParameter("cardno");
        String proname=request.getParameter("proname");
        String uname=request.getParameter("uname");
        String operatorcode=request.getParameter("operatorcode");
        String operatordept=request.getParameter("operatordept");
        String operatorname=request.getParameter("operatorname");
        try {
            Map params=new HashMap();
            if (StringTools.trimNotEmpty(cardno)){
                params.put("cardno",cardno);
            }
            if (StringTools.trimNotEmpty(operatorcode)){
                params.put("operatorcode",operatorcode);
            }
            if(StringTools.trimNotEmpty(proname)){
                params.put("proname",proname);
            }
            if(StringTools.trimNotEmpty(operatordept)){
                params.put("operatordept",operatordept);
            }
            if(StringTools.trimNotEmpty(operatorname)){
                params.put("operatorname",operatorname);
            }
            if(StringTools.trimNotEmpty(uname)){
                params.put("name",uname);
            }
            Map remap = systemLogService.getSystemLogStatisticsListPage(perpage,page, params);
            List<Map> logs= (List<Map>) remap.get("list");
            List<SystemLog> logList=new ArrayList<>();
            if(logs.size()>0){
                for (Map info : logs) {
                    Map map=new HashMap();
                    SystemLog log=new SystemLog();
                    String flowname=info.get("FNAME")==null?info.get("NICKNAME").toString():info.get("FNAME").toString();
                    log.setLogid(info.get("LOGID").toString());
                    log.setProid(info.get("PROID").toString());
                    log.setFid(info.get("FID").toString());
                    log.setRid(info.get("RID").toString());
                    log.setRequesturl(info.get("REQUESTURL").toString());
                    log.setLogtype(info.get("LOGTYPE").toString());
                    log.setLoginip(info.get("LOGINIP").toString());
                    log.setOperatorid(info.get("OPERATORID").toString());
                    log.setOperatorcode(info.get("OPERATORCODE").toString());
                    log.setOperatorname(info.get("OPERATORNAME").toString());
                    log.setOperattime(info.get("OPERATTIME").toString());
                    log.setLogcontent(info.get("LOGCONTENT").toString());
                    log.setUsercustom1(info.get("PRONAME").toString());
                    log.setUsercustom2(flowname);
                    log.setUsercustom3(info.get("CARDNO").toString()+"\t");
                    log.setUsercustom4(info.get("NAME").toString());
                    log.setUsercustom5(info.get("JNAME").toString());
                    log.setUsercustom6(info.get("OPERATORDEPT").toString());
                    SystemLogUtil.fillLogRsultAndRemark(flowname,log);
                    logList.add(log);
                }
            }
            resultBean.setState(true);
            resultBean.setMessage("操作成功");
            Double pageCount = (Double) remap.get("pageCount");
            BigDecimal allcount = (BigDecimal) remap.get("count");
            resultBean.setPageNumber(String.valueOf(perpage));
            resultBean.setTotalResult(String.valueOf(allcount));
            resultBean.setPageSize(String.valueOf(page));
            resultBean.setTotalPage(String.valueOf(pageCount));
            resultBean.setDatas(logList);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return  resultBean;
    }

}
